package com.millstein.educms.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.millstein.commonutils.Result;
import com.millstein.educms.entity.CrmBanner;
import com.millstein.educms.service.CrmBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-25
 */
@RestController
@RequestMapping("/educms/banneradmin")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService crmBannerService;

    /**
     * 分页查询轮播图
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("pageBanner/{page}/{limit}")
    public Result pageBanner(@PathVariable("page") int page, @PathVariable("limit") int limit) {
        Page<CrmBanner> pageBanner = new Page<>(page, limit);
        crmBannerService.page(pageBanner, null);
        return Result.ok().data("items", pageBanner.getRecords()).data("total", pageBanner.getTotal());
    }

    /**
     * 新增轮播图
     * @param crmBanner
     * @return
     */
    @PostMapping
    public Result addBanner(@RequestBody CrmBanner crmBanner) {
        crmBannerService.save(crmBanner);
        return Result.ok();
    }

    /**
     * 根据id查询轮播图的信息
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public Result get(@PathVariable("id") String id) {
        CrmBanner banner = crmBannerService.getById(id);
        return Result.ok().data("item", banner);
    }

    /**
     * 修改轮播图
     * @param crmBanner
     * @return
     */
    @PutMapping
    public Result updateById(@RequestBody CrmBanner crmBanner) {
        crmBannerService.updateById(crmBanner);
        return Result.ok();
    }

    /**
     * 根据id删除轮播图
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable("id") String id) {
        crmBannerService.removeById(id);
        return Result.ok();
    }
}

